$(function() {

  // //获取法律文书类别
  // var knowledge_type_url = jumpUrl+'/knowledge/document/categories';
  // // var knowledge_type_url = 'http://test.elvtuan.com/knowledge/document/categories';
  // $.get(knowledge_type_url, function(data){
          
  //       }, "json").error(function(data) {
  //         var status_code = $.parseJSON(data.responseText);
  //         layer.msg(status_code.message);
  //       }).success(function(data) {
  //          var obj = data.data;
  //          console.info(obj);
  //           var work_scope = obj;
  //           var src = '';
  //           $.each(work_scope, function(key, item) {
  //             src+='<a href="javascript:void(0);" data-id="'+item.key+'">'+item.value+'</a>';
  //           });
  //           $(".type-box").append(src);
  // });
  var searchval = "";
  $(".search-input").focus(function(){  // 获得焦点
        $(".search-input").css({"border":"1px solid #48b4ff"});
        $(".search-icon").css("background","url("+jumpUrl+"/img/question2/searchs.png)");
        // // $(".search-input").change(function(){
        //   // 定时自动搜索
          
        //   var id = setInterval(function(){
        //     cu_params = $(".search-input").val();
        //     if (cu_params!==""&cu_params!==searchval) {
        //       initialization();
        //       geturl();
        //       searchval = $(".search-input").val();
        //       clearInterval(id);
        //     }
        //   },3000);
        // // })
        
      }).blur(function(){     // 失去焦点
        $(".search-input").css({"border":"1px solid #e8e8e8"});
        $(".search-icon").css("background","url("+jumpUrl+"/img/question2/search.png)");
  });
      //搜索
  $('.search-input').bind('input propertychange', function(e) {  
      runTiming(); 
  });
  //执行搜索的全局方法
  var timePromise;
  var runTiming = function(){
    if(timePromise){
        clearTimeout(timePromise);
        timePromise = undefined;
    }
    timePromise = setTimeout(function(){
        initialization();
        geturl();
    }, 2000);
    return timePromise;
  }
  

  // 请求列表
  // get_question_list();
  function get_question_list(question_list_url){

    var loading = '<div class="loading">加载中...</div>';
    $(".lawyer_file_box").append(loading);
    $.get(question_list_url,function(data){
      },'json').error(function(data){
        var obj = $.parseJSON(data.responseText);
        // layer.msg(obj.error_messages);
      }).success(function(data){
        $(".loading").remove();
        var obj = data.data;
        var length = obj.length;
        console.info(obj);
        var str = '';
        if (obj.length>7) {
          $(".load-more").css("display","block")
        }else{
          $(".load-more").css("display","none")
        }
        
        if (obj.length>0) {
          for(var i = 0;i < obj.length; i++){
            str +='<li>'
              str +='<div class="fl"><a href="'+obj[i].img+'" target="_blank"><img src="'+obj[i].img+'" width="100" height="150" style="border: 1px solid #e8e8e8;"></a></div>'
              str +='<div class="fl lawyer-file-info">'
                str +='<p class="knowledge-title">'+obj[i].title+'</p>'
                str +='<p class="font16 colorE">使用说明</p>'
                str +='<p class="font16 colorF">'+obj[i].info+'</p>'
                // if (obj[i].info.length>70) {
                //     str +='<p class="font16 colorF">'+obj[i].info.substr(0,67)+'...</p>'
                // }else{
                //     str +='<p class="font16 colorF">'+obj[i].info+'</p>'
                // }
              str +='</div>'
              str +='<div class="fl">'
                str +='<div class="colorD font14">浏览 <span class="colorC">'+obj[i].views_count+'</span> • 下载 <span class="colorC">'+obj[i].downs+'</span></div>'
                str +='<div style="margin-top:50px;"><a href="'+obj[i].file_path+'" class="knowledge-dow">下载文书</a></div>'
              str +='</div>'
            str +='</li>'
          }
          $(".lawyer_file_box").append(str);
        }else{
        str ='<li class="textac"><span class="noData2"></span></li>';
        $(".lawyer_file_box").append(str);
      }
      });
  }

  // 初始化
  function initialization(){
    $(".lawyer_file_box li").remove();
    $(".loading").remove();
    $(".load-more").attr("data-id",1);
  }
  // 请求的url
  geturl();
  function geturl(){
   var cat_id = $(".type-box a.cuB").attr("data-id");
   var params = $(".search-input").val();
   var index = $(".load-more").attr("data-id");
   var order_params = $(".type-nav2 .cuB").attr("data-id");
   if (cat_id == undefined) {cat_id=""};
   if (order_params == undefined) {order_params=""};
   if (index == "1") {index = ""};

   var paramss = [];
      // paramss.push('type=question');
      paramss.push('size=8');
      if (!cat_id=="") {
      paramss.push("cat_id="+cat_id);
      }
      if (!order_params=="") {
        paramss.push("order="+order_params);
      }
      if (!params=="") {
        paramss.push("q="+params);
      }
      if (!index=="") {
        paramss.push("page="+index);
      }
      paramss = paramss.join('&');
      var thisUrl = location.href.split("?")[0]+'?'+paramss;
      window.history.pushState('obj', document.title, thisUrl);
    var question_list_url= urldow+"?cat_id="+cat_id+"&order="+order_params+"&q="+params+"&rows_per_page=8&page="+index;
    console.info(question_list_url)
    get_question_list(question_list_url);
  }

  // 类别搜索
  $(document).on("click",".type-box a",function(){
      var type_box = $(this).attr("class");
      if (type_box !== "cuB") {
        $(".type-box a").removeClass("cuB");
        $(this).addClass("cuB");
      }else{
        $(".type-box a").removeClass("cuB");
      }
      initialization();
      geturl();
  });
  // 收藏最多
  $(document).on("click",".collectMax",function(post_category){
      var collectMax = $(".collectMax.cuB").val();
      if (collectMax == undefined) {
        $(".collectMax").addClass("cuB");
        $(".replyMax").removeClass("cuB");
      }else{
        $(".collectMax").removeClass("cuB");
      }
      initialization();
      geturl();
  });

  // 回答最多
  $(document).on("click",".replyMax",function(post_category){
      var replyMax = $(".replyMax.cuB").val();
      if (replyMax == undefined) {
        $(".collectMax").removeClass("cuB");
        $(".replyMax").addClass("cuB");
      }else{
        $(".replyMax").removeClass("cuB");
      }
      
      initialization();
      geturl();
  });

  // 加载更多
  $(document).on("click",".load-more",function(e){
      index = parseInt($(".load-more").attr("data-id"))+1;
      $(".load-more").attr("data-id",index);
      geturl();
  });

  // 点击搜索图标
  $(".search-icon").click(function(){
      initialization();
      geturl();
  });
  // 点击搜索
  $(document).on("click","#subSearch",function(e){
      var keyword = $("#keyword").val();
      $(".js-appendContxt").empty();
      if (keyword != null && keyword.length != 0) {
        params ='&q='+keyword;
      }else{
        params =''
      }
      // window.location.href="{{ Config::get('app.url') }}/question/create?"+params;
      getAllUrl(params);
  });
  // enter按键搜索
  var j = 0;
    document.onkeydown = function(e) {
        var keyword = $("#keyword").val();
        var params ='q='+keyword;
        var e = event || window.event || arguments.callee.caller.arguments[0],
         keycode = e.which || e.keyCode;
         if (keycode==13) {
            if (keyword != null && keyword.length != 0) {
            j++;
                if(!j==0){
                  var keyword = $("#keyword").val();
                  $(".js-appendContxt").empty();
                  if (keyword != null && keyword.length != 0) {
                    params ='&q='+keyword;
                  }else{
                    params =''
                  }
                  getAllUrl(params);
                }
            } 
            return false; 
         }else{
            return true;
         }
         stopDefault(e);
    }

});

